﻿using System;
using System.Collections.Generic;
using System.Web;
using JsSerialization = System.Web.Script.Serialization;

namespace JTicket.UI.Extention
{
    public class JsonHelper
    {
        /// <param name="_object"></param>
        /// <returns></returns>
        public static string ObjectToJson(object _object)
        {
            if (_object == null) return string.Empty;
            JsSerialization.JavaScriptSerializer ser = new JsSerialization.JavaScriptSerializer();
            return ser.Serialize(_object);

        }
        /// <summary>
        /// 泛型方法,返回反序列化的T对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString)) return default(T);

            JsSerialization.JavaScriptSerializer ser = new JsSerialization.JavaScriptSerializer();

            return ser.Deserialize<T>(jsonString);
        }
    }
}



